package algorithm;

import java.util.Arrays;

/**
 * 左右指针解题
 */
public class LeftRightIndex {

    // 字符串反转
    public static char[] reverseString(char[] s) {
        int left=0;
        int right=s.length-1;
        while(left<right){
            char temp = s[left];
            s[left] = s[right];
            s[right] = temp;
            left++;
            right--;
        }
        return s;
    }

    // 二分法查找
    public static int search(int[] nums, int target) {
        int left = 0, right = nums.length - 1;
        while (left <= right) {
            int mid = (right - left) / 2 + left;
            int num = nums[mid];
            if (num == target) {
                return mid;
            } else if (num > target) {
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        char[] chars = {'h', 'e', 'l', 'l', 'o'};
        System.out.println(Arrays.toString(reverseString(chars)));

        System.out.println(search(new int[]{-1,0,3,5,9,12},5));
    }
}
